package cn.haocxy.jfm.gui;

import javafx.scene.Node;
import cn.haocxy.jfm.filelist.FileItem;

/**
 * 管理要打开哪个菜单
 * 
 * @author 蔡心宇
 *
 */
public class FileItemMenuManager {

	private FileItemMenu menuSingle = null;
	
	private FileItemMenu menuMulti = null;

	// 当前打开的菜单, 值等于menuSingle或menuMulti
	private FileItemMenu currentMenu = null;
	
	private FileItemMenu single() {
		if (menuSingle == null) {
			menuSingle = new FileItemMenuSingle();
		}
		return menuSingle;
	}
	
	private FileItemMenu multi() {
		if (menuMulti == null) {
			menuMulti = new FileItemMenuMulti();
		}
		return menuMulti;
	}
	
	public void showMenu(IconArea iconArea, Node node, double screenX, double screenY) {
		if (shouldShowSingle(iconArea)) {
			currentMenu = single();
		} else {
			currentMenu = multi();
		}
		currentMenu.setOnShown(()->{
			iconArea._menuShown();
		});
		currentMenu.setOnHidden(()->{
			iconArea._menuHidden();
			currentMenu.setOnShown(null);
			currentMenu.setOnHidden(null);
			currentMenu = null;
		});
		currentMenu.show(iconArea, node, screenX, screenY);
		
	}
	
	private boolean shouldShowSingle(IconArea iconArea) {
		FileItem item = iconArea.getFileItem();
		if (item.inSelectGroup() && item.getFileListModel().selectedNum() > 1) {
			return false;
		} else {
			return true;
		}
	}
	
	public void hideMenu() {
		if (currentMenu != null) {
			currentMenu.hide();
			currentMenu = null;
		}
	}
	
	public boolean shown() {
		return currentMenu != null;
	}
}
